package cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinventorysku;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.erp.customerinformation.vo.CustomerInformationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinventorysku.vo.WarehouseInventorySkuPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinventorysku.vo.WarehouseInventorySkuRespVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinventorysku.vo.WarehouseInventorySkuSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseinventorysku.WarehouseInventorySkuDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.framework.CommonConstant;
import cn.iocoder.yudao.module.system.service.erp.customermanage.CustomerManageService;
import cn.iocoder.yudao.module.system.service.erp.warehouse.WarehouseService;
import cn.iocoder.yudao.module.system.service.erp.warehouseinventorysku.WarehouseInventorySkuService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static java.util.Arrays.asList;

@Tag(name = "管理后台 - 仓库库存sku")
@RestController
@RequestMapping("/erp/warehouse-inventory-sku")
@Validated
public class WarehouseInventorySkuController {
    @Resource
    private AdminUserService userService;
    @Resource
    private CustomerManageService customerManageService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private WarehouseInventorySkuService warehouseInventorySkuService;

    @PostMapping("/create")
    @Operation(summary = "创建仓库库存sku")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-inventory-sku:create')")
    public CommonResult<Long> createWarehouseInventorySku(@Valid @RequestBody WarehouseInventorySkuSaveReqVO createReqVO) {
        return success(warehouseInventorySkuService.createWarehouseInventorySku(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓库库存sku")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-inventory-sku:update')")
    public CommonResult<Boolean> updateWarehouseInventorySku(@Valid @RequestBody WarehouseInventorySkuSaveReqVO updateReqVO) {
        warehouseInventorySkuService.updateWarehouseInventorySku(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库库存sku")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:warehouse-inventory-sku:delete')")
    public CommonResult<Boolean> deleteWarehouseInventorySku(@RequestParam("id") Long id) {
        warehouseInventorySkuService.deleteWarehouseInventorySku(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库库存sku")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-inventory-sku:query')")
    public CommonResult<WarehouseInventorySkuRespVO> getWarehouseInventorySku(@RequestParam("id") Long id) {
        WarehouseInventorySkuDO warehouseInventorySku = warehouseInventorySkuService.getWarehouseInventorySku(id);
        WarehouseInventorySkuRespVO result = BeanUtils.toBean(warehouseInventorySku, WarehouseInventorySkuRespVO.class);
        if (ObjectUtil.isNotNull(result)){
            result.setWarehouse(warehouseService.getWarehouse(result.getWarehouseId()).getName());
            result.setCustomer(userService.getUser(result.getCustomerId()).getNickname());
        }

        return success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓库库存sku分页")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-inventory-sku:query')")
    public CommonResult<PageResult<WarehouseInventorySkuRespVO>> getWarehouseInventorySkuPage(@Valid WarehouseInventorySkuPageReqVO pageReqVO) {
        Long loginUserId= SecurityFrameworkUtils.getLoginUserId();
        AdminUserDO user=userService.getUser(loginUserId);
        String roleCode=user.getRoleCode();
        PageResult<WarehouseInventorySkuRespVO> pageResult =new PageResult<>();
        if(ObjectUtil.equal(loginUserId,1L)||asList(CommonConstant.ERP_ADMIN,CommonConstant.ERP_SUPER_ADMIN).contains(roleCode)) {
            List<Long> customerIds = new ArrayList<>();
            if (ObjectUtil.contains(CommonConstant.ERP_ADMIN, roleCode)) {
                customerIds = customerManageService.getCustomerIds(loginUserId);
            }
            pageReqVO.setCustomerIds(customerIds);
        }
        pageResult=BeanUtils.toBean( warehouseInventorySkuService.getWarehouseInventorySkuPage(pageReqVO), WarehouseInventorySkuRespVO.class);
        pageResult.getList().forEach(e->{
            e.setWarehouse(warehouseService.getWarehouse(e.getWarehouseId()).getName());
            e.setCustomer(userService.getUser(e.getCustomerId()).getNickname());
        });

        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓库库存sku Excel")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-inventory-sku:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarehouseInventorySkuExcel(@Valid WarehouseInventorySkuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarehouseInventorySkuDO> list = warehouseInventorySkuService.getWarehouseInventorySkuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仓库库存sku.xls", "数据", WarehouseInventorySkuRespVO.class,
                        BeanUtils.toBean(list, WarehouseInventorySkuRespVO.class));
    }

}